package servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import Model.Approval;

/**
 * Servlet implementation class ApprovedTable
 */
@WebServlet("/ApprovedTable")
public class ApprovedTable extends HttpServlet {
	private static final long serialVersionUID = 1L;
	JspWriter out;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ApprovedTable() {
		super();
		// TODO Auto-generated constructor stub
	}

	public ApprovedTable(JspWriter outputStream) {
		// TODO Auto-generated constructor stub
		out = outputStream;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		ArrayList<Approval> apptTable = new ArrayList<Approval>();
		apptTable = Database.StaffAppointmentDB.getAllApprovedAppointments("4");

		for (int i = 0; i < apptTable.size(); i++) {
			String description = "-";
			if (!apptTable.get(i).getDescription().equals("Default"))
				description = apptTable.get(i).getDescription();
			//try {
				//if (checkExpire(apptTable.get(i).getDate())) {
					out.print("<tr id=\"apptTable1\" class='"+apptTable.get(i).getAppointmentID()+"'>");

					out.print("<td>");
					out.print(apptTable.get(i).getName());
					out.print("</td>");

					out.print("<td>");
					out.print(apptTable.get(i).getAppointment());
					out.print("</td>");
					// Add diploma and year to display
					out.print("<td>");
					System.out.println("APPT TABLE DIPLOMA : " + apptTable.get(i).getDiploma());
					if(apptTable.get(i).getDiploma().equals("0") || apptTable.get(i).getDiploma() == null)
					{
						out.print("");
					}else{
					out.print(apptTable.get(i).getDiploma());
					}
					out.print("</td>");
					
					out.print("<td>");
					if(apptTable.get(i).getYear().equalsIgnoreCase("0")){
						out.print("");
					}else{
					out.print(apptTable.get(i).getYear());
					}
					out.print("</td>");
					
					String responsibility = apptTable.get(i).getResponsibility();
					
					out.print("<td>");
					//out.print("<td id=\"td1\" onClick=\"getResponsiblity();\">");
					//out.print(description);
					String appointmentID = apptTable.get(i).getAppointmentID(); 
					String setJSFunction = " onClick=\"setPopup('" + appointmentID + "', event)\" ";
					out.print("<a href=\"#\" " + setJSFunction + ">");
					out.print("<img src=\" images/info_icon.png \" width=\"16px\" height=\"16px \">");
					out.print("<input id=\"getResponsiblity\" type = \"hidden\" value = \""+responsibility+"\">");
					out.print("</a>");
					out.print("</td>");

					out.print("<td>");
					out.print(formatdate(apptTable.get(i).getDate()));
					out.print("</td>");

					out.print("<td>");
					out.print(apptTable.get(i).getApprovedBy());
					out.print("</td>");

					out.print("<td>");
					// out.print("<form action=\"ApprovedTablePrint\" method=\"POST\">");
					// out.print("<input type =\"hidden\" name=\"check\"  value=\""+
					// apptTable.get(i).getMd5() + "\" />");
					// out.print("<input type=\"submit\" name=\"action\" value=\"Print\"/>");
					// out.print("</form>");
					out.print("<a target=\"_blank\" href=\"ApptPrint?md5="
							+ apptTable.get(i).getMd5()
							+ "\">Print</a>");
					out.print("</td>");

					out.print("</tr>");
				//}
			//} catch (ParseException e) {
				// TODO Auto-generated catch block
			//	e.printStackTrace();
			//}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private String formatdate(String date) {
		String result = date.split(" ")[0] + " " + date.split(" ")[1].charAt(0)
				+ date.split(" ")[1].substring(1).toLowerCase() + " "
				+ date.split(" ")[2];
		return result;
	}

	private boolean checkExpire(String date) throws ParseException {
		// return false if expire
		boolean check = true;

		SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM yyyy");
		Date convertedDate = dateFormat.parse(date);

		Date now = new Date();

		int diffInDays = (int) ((now.getTime() - convertedDate.getTime()) / (1000 * 60 * 60 * 24));

		int noOfDay = Integer.parseInt(Logic.SetApptExpire_BLL
				.getCurrentNumber());
		if (diffInDays > noOfDay) {
			check = false;
		}

		return check;
	}
}
